package com.zed.game.answer.fetures;

import com.zed.api.common.SequenceNumber;
import com.zed.api.games.answer.NotSupportProtocolException;
import com.zed.api.games.answer.scene.AnswerShowWorldRequest;
import com.zed.api.games.answer.scene.QuestionBankScene;
import com.zed.api.games.answer.show.AlternativeAnswer;
import com.zed.api.games.answer.show.AnswerShowWorld;
import com.zed.games.answer.show.TextAnswerShowWorld;
import com.zed.games.answer.show.URLAnswerShowWorld;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

/**
 * 缺省的题库场景
 *
 * @author Zed
 */
public class DefaultQuestionBankScene implements QuestionBankScene {

    private static Logger LOGGER = LoggerFactory.getLogger(DefaultQuestionBankScene.class);

    private SequenceNumber sequenceNumber;

    public DefaultQuestionBankScene(SequenceNumber sequenceNumber) {
        this.sequenceNumber = sequenceNumber == null ? new QuestionBankSequenceNumber() : sequenceNumber;
    }

    /**
     * 确保支持协议
     */
    protected void ensureSupportProtocols(Object theme) throws NotSupportProtocolException {
        if (theme instanceof String) {
            String value = theme.toString();
            if (!value.startsWith("http") || !value.startsWith("https") || !value.startsWith("text")) {
                throw new NotSupportProtocolException("未知类型 => ".concat(value));
            }
        }
    }

    @Override
    public List<AnswerShowWorld> add(AnswerShowWorldRequest... requests) throws NotSupportProtocolException {
        List<AnswerShowWorld> result = new ArrayList<>();

        for (AnswerShowWorldRequest request : requests) {
            this.ensureSupportProtocols(request.getTheme());
        }

        for (AnswerShowWorldRequest request : requests) {

            String theme = request.getTheme().toString();

            String id = String.valueOf(this.getSequenceNumber().next());

            AnswerShowWorld.Builder builder = null;

            if (theme.startsWith("http") || theme.startsWith("https")) {
                builder = URLAnswerShowWorld.newBuilder().url(theme);
            } else {
                builder = TextAnswerShowWorld.newBuilder().text(theme);
            }

            builder.id(id);

            for (AlternativeAnswer alternativeAnswer : request.getMergeAlternativeAnswer()) {
                builder.addAlternativeAnswer(alternativeAnswer);
            }

            for (String tip : request.getTips()) {
                builder.addTips(tip);
            }

            try {
                AnswerShowWorld answerShowWorld = builder.build();
                result.add(answerShowWorld);
            } catch (Exception e) {
                LOGGER.warn("添加题目异常 => {}", theme, e);
            }
        }



        return result;
    }

    @Override
    public void delete(List<String> ids) {

    }

    /**
     * 获取序列器
     *
     * @return {@link SequenceNumber}
     */
    public SequenceNumber getSequenceNumber() {
        return this.sequenceNumber;
    }
}
